package com.zhuozhou.jdyc.main.main.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.CartShopItemBinding;
import com.zhuozhou.jdyc.main.main.bean.BasicsShoppingBean;
import com.zhuozhou.jdyc.util.apiUtil.NumberUtils;
import com.zhuozhou.jdyc.wight.dialog.CouponDialog;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by CherishTang on 2020/3/27 0027
 * describe:首页菜单adapter
 */
public class CartShopAdapter extends BaseRecyclerViewAdapter<BasicsShoppingBean.ObjBeanX.ObjBean, CartShopItemBinding> {

    private int type = 0;

    public CartShopAdapter(Context context) {
        super(context);
    }

    public OnItemSetMoneyListener mOnItemSetMoneyListener;
    public OnCouponItemClickListener mOnCouponItemClickListener;

    @Override
    protected int getLayoutId() {
        return R.layout.cart_shop_item;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);

    }

    @Override
    public void onBindHolder(BaseViewHolder<CartShopItemBinding> holder, int pos) {
        setMoney(holder.getBinding(), pos);
        CartShopItemAdapter cartShopItemAdapter = new CartShopItemAdapter(mContext);
        if (mList.get(pos).isChecked()) {
            List<BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean> list = new ArrayList<>();
            for (BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean item : mList.get(pos).getShangpin()) {
                item.setChecked(mList.get(pos).isChecked());
                list.add(item);
            }
            mList.get(pos).setShangpin(list);
        }
        cartShopItemAdapter.setType(type);
        cartShopItemAdapter.setList(mList.get(pos).getShangpin());
        cartShopItemAdapter.setOnItemClickRadioListener((p, position) -> {
            if (position == -3) {
                mOnItemSetMoneyListener.onItemSetMoney(pos, p, 4);
            } else if (position == -2) {
                mOnItemSetMoneyListener.onItemSetMoney(pos, p, 1);
            } else if (position == -1) {
                mOnItemSetMoneyListener.onItemSetMoney(pos, p, 2);
                setMoney(holder.getBinding(), pos);
            } else if (position >= 0) {
                if (getIsAllChecked(pos)) {
                    holder.getBinding().radioMerchant.setChecked(true);
                    mList.get(pos).setChecked(true);
                } else {
                    holder.getBinding().radioMerchant.setChecked(false);
                    mList.get(pos).setChecked(false);
                }
                mOnItemSetMoneyListener.onItemSetMoney(-1, -1, 0);
            }
        });
        holder.getBinding().mRecyclerViewShop.setAdapter(cartShopItemAdapter);
        holder.getBinding().mRecyclerViewShop.setLayoutManager(new MyLayoutManager(mContext));
        holder.getBinding().setBean(mList.get(pos));
        holder.getBinding().tvMoneyOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中间
        holder.getBinding().radioMerchant.setChecked(mList.get(pos).isChecked());
        holder.getBinding().radioMerchant.setOnCheckedChangeListener((buttonView, isChecked) -> {
            List<BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean> list = new ArrayList<>();
            for (BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean item : mList.get(pos).getShangpin()) {
                item.setChecked(isChecked);
                list.add(item);
            }
            mList.get(pos).setShangpin(list);
            mList.get(pos).setChecked(isChecked);
            cartShopItemAdapter.setList(mList.get(pos).getShangpin());
            cartShopItemAdapter.notifyDataSetChanged();
            mOnItemSetMoneyListener.onItemSetMoney(-1, -1, 0);
        });
        holder.getBinding().llTitle.setOnClickListener(v -> {
            new CouponDialog(mContext)
                    .setText("去使用")
                    .setList(mList.get(pos).getDiscount())
                    .builder()
                    .setOnSureClickListener(i -> {
//                        if (DateUtil.getDateByFormat(i.getDate(), "yyyy-MM-dd HH:mm:ss").compareTo(new Date()) <=
//                                0 && DateUtil.getDateByFormat(i.getDate2(), "yyyy-MM-dd HH:mm:ss").compareTo(new Date()) >= 0) {
                        mOnCouponItemClickListener.OnCouponItemClick(pos, i.getId());
//                        } else {
//                            ToastUtils.showShort("不在有效期内");
//                        }
                    })
                    .show();
        });
        if (mList.get(pos).getDiscount() != null && mList.get(pos).getDiscount().size() > 0) {
            if (mList.get(pos).getDiscountSelect() != null) {
                holder.getBinding().tvCoupon.setText(mList.get(pos).getDiscountSelect().getName());
            } else
                holder.getBinding().tvCoupon.setText("有可用优惠券");
            holder.getBinding().llTitle.setEnabled(true);
        } else {
            holder.getBinding().tvCoupon.setText("暂无优惠劵");
            holder.getBinding().llTitle.setEnabled(false);
        }
        holder.getBinding().tvMerchant.setOnClickListener(v -> {
            mOnItemSetMoneyListener.onItemSetMoney(pos, -1, 3);
        });
    }

    private void setMoney(CartShopItemBinding binding, int pos) {
        String[] strings = getAllMoney(mList.get(pos).getShangpin());
        if (type != 0 && type != 1) {
            binding.llMoney0.setVisibility(View.VISIBLE);
            binding.llTitle.setVisibility(View.GONE);
            binding.llMoney1.setVisibility(View.GONE);
            if (type == 2)
                binding.setPrice2(NumberUtils.decimalFormat(strings[1] + "积分 + ￥" + strings[0] + ""));//优惠后
            else
                binding.setPrice2(NumberUtils.decimalFormat(strings[1] + "易货积分"));//优惠后
        } else {
            binding.llMoney0.setVisibility(View.GONE);
            binding.llMoney1.setVisibility(View.VISIBLE);
            binding.llTitle.setVisibility(View.VISIBLE);
            binding.setPrice(NumberUtils.decimalFormat("￥" + strings[0] + ""));//优惠前
            binding.setPrice2(NumberUtils.decimalFormat("￥" + getDiscountMoney(strings[0], pos) + ""));//优惠后
        }
    }

    private String getDiscountMoney(String money, int pos) {
        if (mList.get(pos).getDiscountSelect() == null || mList.get(pos).getDiscountSelect().getPrice2() == null) {
            return money;
        } else if (Double.parseDouble(money) <= Double.parseDouble(mList.get(pos).getDiscountSelect().getPrice2())) {
            return money;
        } else {
            return NumberUtils.decimalFormat((Double.parseDouble(money) - Double.parseDouble(mList.get(pos).getDiscountSelect().getPrice())) + "");
        }
    }

    private String[] getAllMoney(List<BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean> bean) {
        Double allMoney = 0.00;
        Double allIn = 0.00;
        if (bean.size() == 0) {
            return new String[]{NumberUtils.decimalFormat(String.valueOf(allMoney)), allIn == 0 ? "0" : allIn + ""};
        }
        for (BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean shop : bean) {
            if (type == 1 || type == 2)
                allMoney = (allMoney + (shop.getNumber() * Double.parseDouble(shop.getGoods_price())));
            if (type == 2)
                allIn = (allIn + (shop.getNumber() * Double.parseDouble(shop.getIntegral())));
            if (type == 3)
                allIn = (allIn + (shop.getNumber() * Double.parseDouble(shop.getYibi())));
        }
        return new String[]{NumberUtils.decimalFormat(String.valueOf(allMoney)), allIn == 0 ? "0" : allIn + ""};
    }

    private boolean getIsAllChecked(int pos) {
        if (mList.get(pos).getShangpin() == null) return false;
        if (mList.get(pos).getShangpin().size() == 0) return false;
        int cont = 0;
        for (BasicsShoppingBean.ObjBeanX.ObjBean.ShangpinBean item : mList.get(pos).getShangpin()) {
            if (item.isChecked()) cont++;
        }
        if (cont == mList.get(pos).getShangpin().size())
            return true;
        else return false;
    }

    public void setOnItemSetMoneyListener(OnItemSetMoneyListener l) {
        mOnItemSetMoneyListener = l;
    }

    public interface OnItemSetMoneyListener {
        void onItemSetMoney(int ps, int p, int type);
    }

    public void setOnCouponItemClickListener(OnCouponItemClickListener l) {
        mOnCouponItemClickListener = l;
    }

    public interface OnCouponItemClickListener {
        void OnCouponItemClick(int position, Integer couponId);
    }

    public void setType(int type) {
        this.type = type;
    }

}
